﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Aspose.Cells;
using Aspose.Pdf;
using Aspose.Words;
using MyDMS.DAL;
using Document = MyDMS.DAL.Document;

public partial class ViewDocument : System.Web.UI.Page
{
    MyDMSEntity _myDmsEntity=new MyDMSEntity();
    private int documentCode = 0;
    private string filePath = string.Empty;
    protected string documentData = string.Empty;

    private FileInfo file;
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.QueryString["doc"]!=null)
        {
            documentCode = Convert.ToInt32(Request.QueryString["doc"]);
            Document upDoc = _myDmsEntity.Document.FirstOrDefault(ud => ud.Document_Code == documentCode);
            filePath = upDoc.File_Path;
            file = new FileInfo(filePath);
            if(!file.Exists)
            {
                Response.Write("文件已经删除");
            }
            switch (file.Extension.ToLower())
            {
                case ".doc":
                case ".docx":
                    Aspose.Words.Document d = new Aspose.Words.Document(filePath);
                    MemoryStream stream1 = new MemoryStream();
                    d.Save(stream1, SaveFormat.Html);

                    Label1.Text = System.Text.Encoding.UTF8.GetString(stream1.ToArray());
                    break;
                case ".xls":
                case ".xlsx":
                    Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                    workbook.Open(filePath);
                    MemoryStream stream2 = new MemoryStream();
                    workbook.Save(stream2, FileFormatType.Html);
                    Label1.Text = System.Text.Encoding.UTF8.GetString(stream2.ToArray());
                    break;
                default:
                    Response.ClearContent();
                    Response.ClearHeaders();
                    Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}", file.Name));
                    Response.AppendHeader("content-type", ReturnExtension(file.Extension.ToLower()));
                    Response.WriteFile(filePath);
                    Response.Flush();
                    Response.Close(); 
                    break;
            }
        }
    }

    private void DownloadFile(string filepath,bool isdownload)
    {
        //http://dotnetslackers.com/Community/blogs/haissam/archive/2007/04/03/Downloading-Files-C_2300_.aspx

        // Get the physical Path of the file(test.doc)
        //string filepath = );

        // Create New instance of FileInfo class to get the properties of the file being downloaded
        file = new FileInfo(filepath);

        // Checking if file exists
        if (file.Exists)
        {
            // Clear the content of the response
            Response.ClearContent();

            if(isdownload)
            {
            Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
            }
            else
            {
            // LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
            Response.AddHeader("Content-Disposition", "inline; filename=" + file.Name);
            }

            // Add the file size into the response header
            Response.AddHeader("Content-Length", file.Length.ToString());

            // Set the ContentType
            Response.ContentType = ReturnExtension(file.Extension.ToLower());

            // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
            Response.Write(file.FullName);

            // End the response
            Response.End();
        }
    }

    private string ReturnExtension(string fileExtension)
    {
        switch (fileExtension)
        {
            case ".htm":
            case ".html":
            case ".log":
                return "text/HTML";
            case ".txt":
                return "text/plain";
            case ".doc":
                return "application/msword";
            case ".tiff":
            case ".tif":
                return "image/tiff";
            case ".asf":
                return "video/x-ms-asf";
            case ".avi":
                return "video/avi";
            case ".zip":
                return "application/zip";
            case ".xls":
            case ".csv":
                return "application/vnd.ms-excel";
            case ".gif":
                return "image/gif";
            case ".jpg":
            case "jpeg":
                return "image/jpeg";
            case ".bmp":
                return "image/bmp";
            case ".wav":
                return "audio/wav";
            case ".mp3":
                return "audio/mpeg3";
            case ".mpg":
            case "mpeg":
                return "video/mpeg";
            case ".rtf":
                return "application/rtf";
            case ".asp":
                return "text/asp";
            case ".pdf":
                return "application/pdf";
            case ".fdf":
                return "application/vnd.fdf";
            case ".ppt":
                return "application/mspowerpoint";
            case ".dwg":
                return "image/vnd.dwg";
            case ".msg":
                return "application/msoutlook";
            case ".xml":
            case ".sdxl":
                return "application/xml";
            case ".xdp":
                return "application/vnd.adobe.xdp+xml";
            default:
                return "application/octet-stream";
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if(!string.IsNullOrEmpty(filePath))
        {
            DownloadFile(filePath,true);
        }
    }
}